<template>
  <div class="myCenter in-up-container">
    <el-image
      class="my-el-image"
      style="position: absolute"
      :src="$constant.two_poem_image[Math.floor(Math.random() * $constant.two_poem_image.length)]"
      lazy
      fit="none"
    >
      <div
        slot="error"
        class="image-slot"
      ></div>
    </el-image>
    <div
      class="in-up"
      id="loginAndRegist"
    >
      <div class="form-container sign-up-container">
        <div class="myCenter">
          <h1>注册</h1>
          <input
            v-model="username"
            type="text"
            maxlength="30"
            placeholder="用户名"
          >
          <input
            v-model="password"
            type="password"
            maxlength="30"
            placeholder="密码"
          >
          <input
            v-model="email"
            type="email"
            placeholder="邮箱"
          >
          <input
            v-model="code"
            type="text"
            placeholder="验证码"
            disabled
          >
          <a
            style="margin: 0"
            href="#"
            @click="changeDialog('邮箱验证码')"
          >获取验证码</a>
          <button @click="regist()">注册</button>
        </div>
      </div>
      <div class="form-container sign-in-container">
        <div class="myCenter">
          <h1>登录</h1>
          <input
            v-model="account"
            type="text"
            placeholder="用户名/邮箱/手机号"
          >
          <input
            v-model="password"
            type="password"
            placeholder="密码"
          >
          <a
            href="#"
            @click="changeDialog('找回密码')"
          >忘记密码？</a>
          <button @click="login()">登录</button>
        </div>
      </div>
      <div class="overlay-container">
        <div class="overlay">
          <div class="overlay-panel myCenter overlay-left">
            <h1>已有帐号？</h1>
            <p>请登录🚀</p>
            <button
              class="ghost"
              @click="signIn()"
            >登录</button>
          </div>
          <div class="overlay-panel myCenter overlay-right">
            <h1>没有帐号？</h1>
            <p>立即注册吧😃</p>
            <button
              class="ghost"
              @click="signUp()"
            >注册</button>
          </div>
        </div>
      </div>
    </div>
  </div>

</template>


<script>
export default {
  name: "login",
  data() {
    return {
      // currentUser: this.$store.state.currentUser,
      username: "",
      account: "",
      password: "",
      phoneNumber: "",
      email: "",
      avatar: "",
      showDialog: false,
      code: "",
      dialogTitle: "",
      codeString: "验证码",
      passwordFlag: null,
      intervalCode: null,
    };
  },
  methods: {
    addPicture(res) {
      this.avatar = res;
      this.submitDialog();
    },
    signUp() {
      document
        .querySelector("#loginAndRegist")
        .classList.add("right-panel-active");
      console.log(2222222);
    },
    signIn() {
      document
        .querySelector("#loginAndRegist")
        .classList.remove("right-panel-active");
      console.log(1111111);
    },
    // 登录
    async login() {
      if (
        this.$common.isEmpty(this.account) ||
        this.$common.isEmpty(this.password)
      ) {
        this.$message({
          message: "请输入账号或密码！",
          type: "error",
        });
        return;
      }

      let user = {
        account: this.account.trim(),
        password: this.$common.encrypt(this.password.trim()),
      };

      const loginData = JSON.stringify({
        username: this.username.trim(),
        password: this.password.trim(),
      });

      try {
        await this.$store.dispatch("userLogin", loginData);
        let toPath = this.$route.query.redirect;
        if (toPath) {
          this.$router.push(toPath);
        } else {
          this.$router.push("/video-page");
        }
      } catch (error) {
        this.$message({
          message: error.message,
          type: "error",
        });
      }
    },
    // 注册
    async regist() {
      if (
        this.$common.isEmpty(this.username) ||
        this.$common.isEmpty(this.password)
      ) {
        this.$message({
          message: "请输入用户名或密码！",
          type: "error",
        });
        return;
      }

      if (
        this.dialogTitle === "邮箱验证码" &&
        this.$common.isEmpty(this.email)
      ) {
        this.$message({
          message: "请输入邮箱！",
          type: "error",
        });
        return false;
      }

      if (this.$common.isEmpty(this.code)) {
        this.$message({
          message: "请输入验证码！",
          type: "error",
        });
        return;
      }

      if (
        this.username.indexOf(" ") !== -1 ||
        this.password.indexOf(" ") !== -1
      ) {
        this.$message({
          message: "用户名或密码不能包含空格！",
          type: "error",
        });
        return;
      }

      let user = {
        username: this.username.trim(),
        code: this.code.trim(),
        password: this.password.trim(),
      };

      const registerData = JSON.stringify({
        username: this.username.trim(),
        password: this.password.trim(),
      });

      if (this.dialogTitle === "邮箱验证码") {
        user.email = this.email;
      }

      try {
        await this.$store.dispatch("userRegister", registerData);
        // 注册后自动登录
        await this.$store.dispatch("userLogin", user);
        let toPath = this.$route.query.redirect;
        if (toPath) {
          this.$router.push(toPath);
        } else {
          this.$router.push("/video-page");
        }
      } catch (error) {
        this.$message({
          message: error.message,
          type: "error",
        });
      }
    },
  },
};
</script>

<style scoped>
.el-image {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  object-fit: cover;
}

.myCenter {
  display: flex;
  justify-content: center;
  align-items: center;
}

.in-up-container {
  height: 100vh;
  margin: 0;
  position: relative;
}

.in-up {
  opacity: 0.9;
  border-radius: 10px;
  position: relative;
  overflow: hidden;
  width: 750px;
  max-width: 100%;
  min-height: 450px;
  margin: 10px;
}

.in-up p {
  font-size: 14px;
  letter-spacing: 1px;
  margin: 20px 0 30px 0;
}

.in-up a {
  color: rgb(28, 27, 27);
  font-size: 14px;
  text-decoration: none;
  margin: 15px 0;
}

.form-container {
  position: absolute;
  height: 100%;
  transition: all 0.5s ease-in-out;
}

.sign-in-container {
  left: 0;
  width: 50%;
}

.sign-up-container {
  left: 0;
  width: 50%;
  opacity: 0;
}

.form-container div {
  background: white;
  flex-direction: column;
  padding: 0 20px;
  height: 100%;
}

.form-container input {
  background: #eeeeee;
  border-radius: 2px;
  border: none;
  padding: 12px 15px;
  margin: 10px 0;
  outline: none;
}

.in-up button {
  border-radius: 2rem;
  border: none;
  background: #ff4b2b;
  color: white;
  font-size: 16px;
  font-weight: bold;
  padding: 12px 45px;
  letter-spacing: 2px;
  cursor: pointer;
}

.in-up button:hover {
  animation: scale 0.8s ease-in-out;
}

.in-up button.ghost {
  background: transparent;
  border: 1px solid white;
}

.sign-up-container button {
  margin-top: 20px;
}

.overlay-container {
  position: absolute;
  left: 50%;
  width: 50%;
  height: 100%;
  overflow: hidden;
  transition: all 0.5s ease-in-out;
  background-color: #ff4b2b;
}

.overlay {
  background: #ff4b2b;
  color: #eeeeee;
  position: relative;
  left: -100%;
  height: 100%;
  width: 200%;
}

.overlay-panel {
  position: absolute;
  top: 0;
  flex-direction: column;
  height: 100%;
  width: 50%;
  transition: all 0.5s ease-in-out;
}

.overlay-right {
  right: 0;
  transform: translateY(0);
}

.overlay-left {
  transform: translateY(-20%);
}

.in-up.right-panel-active .sign-in-container {
  transform: translateY(100%);
}

.in-up.right-panel-active .overlay-container {
  transform: translateX(-100%);
}

.in-up.right-panel-active .sign-up-container {
  transform: translateX(100%);
  opacity: 1;
}

.in-up.right-panel-active .overlay {
  transform: translateX(50%);
}

.in-up.right-panel-active .overlay-left {
  transform: translateY(0);
}

.in-up.right-panel-active .overlay-right {
  transform: translateY(20%);
}
</style>